import Vue from 'vue'
import VueRouter from 'vue-router'
// import Home from '../views/Home.vue'
import tolist from './page'
// 路由白名单
const whiteList=[
  'Home',
  'Long',
  'Error',
]
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect:'/home',
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import(/* webpackChunkName: "home" */ '../views/home/home.vue'),
    children:tolist
     
   
  },
  {
    path: '/long',
    name: 'Long',
    component: () => import(/* webpackChunkName: "home" */ '../views/long/long.vue')
  },
  {
    path: '/error',
    name: 'Error',
    component: () => import(/* webpackChunkName: "404" */ '../views/error/error.vue')
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// 路由全局守卫
router.beforeEach((to,from,next)=>{
  const showRouter=tolist
  const token=localStorage.getItem('token')
  if(to.path!=='/long'&&!token){
        next('/long')
  }else{
    if(showRouter.some(item=>item.path===to.path)||whiteList.some(item=>item===to.name)){
      next()
    }else{
      next('/error')
    }
   
  }
})

export default router
